package handler

import (
	"ecom-order-sys/cache"
	"ecom-order-sys/model"
	"ecom-order-sys/pkg"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Deng(c *gin.Context) {

	username := c.PostForm("username")
	password := c.PostForm("password")

	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "字段不能为空",
		})
		return
	}

	redis_deng_key := "deng_key" + username
	errcont, _ := cache.Rdb.Get(cache.Ctx, redis_deng_key).Int()
	if errcont >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "密码错误超过三次，禁止登录",
		})
		return
	}

	var data model.User
	if err := model.DB.Where("username = ?", username).Find(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "账号或密码错误",
		})
		return
	}
	if data.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "账号或密码错误",
		})
		return
	}
	if data.Password != pkg.MD5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_deng_key)
		if errcont == 2 {
			cache.Rdb.Set(cache.Ctx, redis_deng_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"smg":  "账号或密码错误",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "账号或密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "登录成功",
		"data": data,
	})
	return
}
func RoleCreate(c *gin.Context) {

	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))

	data := model.RoleNode{
		Rid: rid,
		Nid: nid,
	}

	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 200,
			"smg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "添加成功",
		"data": data,
	})
	return
}
func RoleFind(c *gin.Context) {
	uid := c.PostForm("uid")
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.rid = roles.id
		WHERE
		  user_roles.uid = 1
	*/
	var data model.Role

	model.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid = ?", uid).
		Debug().
		Find(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "查询成功",
		"data": data,
	})
	return
}
func NodeFind(c *gin.Context) {
	rid := c.PostForm("rid")
	/*
		SELECT
		*
		FROM
		nodes
		LEFT JOIN role_nodes ON role_nodes.nid = nodes.id
		WHERE role_nodes.rid = 1
	*/
	var data model.Node

	model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").
		Where("role_nodes.rid = ?", rid).
		Debug().
		Find(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "查询成功",
		"data": data,
	})
	return
}
func PostCreate(c *gin.Context) {

	postname := c.PostForm("postname")
	postjia, _ := strconv.Atoi(c.PostForm("postjia"))
	postzhuang := c.PostForm("postzhuang")

	data := model.Post{
		Postname:   postname,
		Postjia:    postjia,
		Postzhuang: postzhuang,
	}

	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "添加成功",
		"data": data,
	})
	return
}
func AssCreate(c *gin.Context) {

	assbian := c.PostForm("assbian")
	asske := c.PostForm("asske")
	assdi := c.PostForm("assdi")
	pid, _ := strconv.Atoi(c.PostForm("pid"))

	data := model.Ass{
		Assbian: assbian,
		Asske:   asske,
		Assdi:   assdi,
		Pid:     pid,
	}

	if err := model.DB.Create(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"smg":  "添加成功",
		"data": data,
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.PostForm("page"))
	size, _ := strconv.Atoi(c.PostForm("size"))
	offset := (page - 1) * size

	var data []model.List

	/*
			SELECT
		  asses.id AS Id,
		  asses.assbian AS Listbian,
		  asses.asske AS Listke,
		  posts.postname AS Listname,
		  posts.postzhuang AS Listzuang
		  FROM
		  asses
		  LEFT JOIN posts ON posts.id = asses.pid
	*/

	redis_list_key := "list_key" + strconv.Itoa(page)
	errcount, _ := cache.Rdb.Get(cache.Ctx, redis_list_key).Result()
	if errcount == "" {
		model.DB.Table("asses").
			Select(
				"asses.id AS Id",
				"asses.assbian AS Listbian",
				"asses.asske AS Listke",
				"posts.postname AS Listname",
				"posts.postzhuang AS Listzuang",
			).
			Joins("LEFT JOIN posts ON posts.id = asses.pid").
			Offset(offset).Limit(size).Debug().Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_list_key, jsonData, time.Hour*2)

	} else {
		json.Unmarshal([]byte(errcount), &data)
	}

	var total int64
	model.DB.Model(&model.Ass{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"smg":   "列表展示",
		"data":  data,
		"total": total,
	})
	return
}
